/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import dao.StaticsStrings;
import dao.impl.GrowMessages;
import dao.impl.RatingDaoImpl;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import model.Rating;
import model.Usuarios;
import model.Veiculos;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rafael
 */
@ManagedBean
@RequestScoped
public class BeanRating {

    /**
     * Creates a new instance of BeanRating
     */
    private Rating rating;

    public BeanRating() {

        this.rating = new Rating();
    }

    public void inserirRating(Usuarios usuarios, Veiculos veiculos) {
        if (usuarios.getLogin() != null) {
            RatingDaoImpl daoImpl = new RatingDaoImpl();
            if (daoImpl.getRatingUsuario(usuarios.getPkId(), veiculos.getPkVeiculos()) == null) {
                this.rating.setUsuarios(usuarios);
                this.rating.setVeiculos(veiculos);
                this.rating.setRating(1);
                daoImpl.inserir(rating);
                RequestContext.getCurrentInstance().update("outputRating");
            } else {
                GrowMessages.errorMessage(StaticsStrings.OOOPS, StaticsStrings.VOCE_CURTIU);
            }
        } else {
            GrowMessages.errorMessage(StaticsStrings.OOOPS, StaticsStrings.LOGAR_CURTIR);
        }
    }

    public boolean validateCurtir(Integer usuarioId, Integer veiculoId) {

        if (new RatingDaoImpl().getRatingUsuario(usuarioId, veiculoId) == null) {
            return false;
        } else {
            return true;
        }
    }
    /*  if (!logado) {
     return true;
     } else {
     if (usuarioLocal == null) {
     return true;
     } else {
     if (veiculo == null) {
     return true;
     }
     */

    public Rating getRating() {
        return rating;
    }

    public void setRating(Rating rating) {
        this.rating = rating;
    }
}
